package com.simple.sample.web.controller;

import com.simple.core.controller.BaseController;
import com.simple.core.dto.Response;
import com.simple.sample.server.test.TestEntity;
import com.simple.sample.server.test.TestEntityService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author FanXing
 * @date 2023年03月05日 9:38
 */
@Api(tags = "测试")
@RestController
@RequestMapping("/test")
public class TestEntityController extends BaseController<TestEntity, TestEntityService> {

    @GetMapping("/findAll")
    public Response<List<TestEntity>> findAll(TestEntity testEntity) {
        return Response.ok(service.findAll(testEntity));
    }

    @GetMapping("/findAllByIds")
    public Response<List<TestEntity>> findAllByIds(@RequestParam List<Long> ids) {
        return Response.ok(service.findAll(new ArrayList<>(ids)));
    }

    @GetMapping("/findAllBySpec")
    public Response<List<TestEntity>> findAllBySpec(@RequestParam List<Long> ids) {
        return Response.ok(service.findAll(((root, query, criteriaBuilder) -> root.get("id").in(ids))));
    }

    @GetMapping("/findOne")
    public Response<TestEntity> findOne(TestEntity test) {
        return Response.ok(service.findOne(test).orElse(null));
    }

    @GetMapping("/count")
    public Response<Long> count(TestEntity test) {
        return Response.ok(service.count(test));
    }

    @GetMapping("/countBySpec")
    public Response<Long> countBySpec(@RequestParam List<Long> ids) {
        return Response.ok(service.count(((root, query, criteriaBuilder) -> root.get("id").in(ids))));
    }
}
